package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.autoconfig.redis.RedisCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.dto.OrderComplainDto;
import com.yungu.swift.order.model.dto.OrderDto;
import com.yungu.swift.order.model.vo.ApiComplainVo;
import com.yungu.swift.order.model.vo.JsonOrderListVo;
import com.yungu.swift.order.service.OrderComplainService;
import com.yungu.swift.order.service.OrderFareService;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.system.sys.service.SysCompanyService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：司机订单相关操作
 *
 * @author Zerrion
 * @Time 2016/12/15
 */

@Controller
@RequestMapping(value = "/api/v1/driver")
@Api(value = "【司机】【订单】【公用】订单通用接口", tags = "司机订单相关操作")
public class DriOrderApi extends BaseApi {

    @Reference
    private DriverService driverService;
    @Reference
    private OrderService orderService;
    @Reference
    private OrderFareService orderFareService;
    @Reference
    private OrderComplainService orderComplainService;
    @Reference
    private SysCompanyService sysCompanyService;
    @Autowired
    private RedisCacheService redisCacheService;

    @RequestMapping(value = {"/token/complaint/add"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机投诉乘客接口", notes = "保存投诉", response = ResponseData.class)
    public ResponseData<Boolean> addComplaint(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "contents", value = "投诉内容") @RequestParam(value = "contents", required = false) String contents,
            @ApiParam(name = "remark", value = "备注") @RequestParam(value = "remark", required = false) String remark,
            @ApiParam(name = "listenRecording", value = "是否允许调取录音 0不允许 1 允许") @RequestParam(required = false, defaultValue = "0") Integer listenRecording,
            HttpServletRequest request) {
        Map<String, Object> map = MapUtils.build();
        map.put("orderUuid", orderUuid);
        map.put("complainantUuid", getUserUid(request));
        OrderComplainDto complainDto = orderComplainService.get(map).getData();
        if (complainDto != null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "已投诉该乘客！");
        }
        OrderDto orderDto = orderService.get(orderUuid).getData();
        if (orderDto == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到订单信息");
        }
        //新增投诉信息记录
        OrderComplainDto orderComplainDto = new OrderComplainDto();
        orderComplainDto.setUuid(StringUtils.buildUUID());
        orderComplainDto.setOrderUuid(orderUuid);
        orderComplainDto.setComplainantUuid(getUserUid(request));
        orderComplainDto.setRespondentUuid(orderDto.getPassengerUuid());
        orderComplainDto.setRespondentIdentity(CommonConstant.PASSENGER);
        orderComplainDto.setContents(contents);
        orderComplainDto.setRemark(remark);
        orderComplainDto.setListenRecording(listenRecording);
        orderComplainDto.setStatus(OrderComplainDto.STATUS_PENDING);
        orderComplainDto.setAppid(appid);
        orderComplainDto.setCreateOn(new Date());
        ResponseData<Boolean> responseData = orderComplainService.add(orderComplainDto);
        if (responseData.isSuccess()) {
            responseData.setMsg("投诉成功");
        }
        return responseData;
    }

    @RequestMapping(value = {"/token/order/cancel"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机取消订单接口", notes = "取消订单", response = ResponseData.class)
    public ResponseData<Boolean> confirmCancel(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "cancelMsg", value = "取消原因") @RequestParam(required = false) String cancelMsg,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText) {
        if (!StringUtils.isNotEmpty(orderUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数错误");
        }
        return orderService.driConfirmCancel(orderUuid, cancelMsg, uploadText);
    }


    @RequestMapping(value = {"/token/order/cancelDescription"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机取消说明接口", notes = "取消说明", response = ResponseData.class)
    public ResponseData<Map<String, Object>> cancelDescription(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单", required = true) @RequestParam String orderUuid) {
        if (!StringUtils.isNotEmpty(orderUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数错误");
        }
        return orderFareService.getWaitFare(orderUuid);
    }


    @RequestMapping(value = {"/token/order/rushFare",}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机催款接口", notes = "司机催款", response = ResponseData.class)
    public ResponseData<Boolean> rushFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            HttpServletRequest request) {
        return orderFareService.rushFare(orderUuid, getUserUid(request));

    }

    @RequestMapping(value = {"/token/order/isComplain"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机查询订单投诉状态", notes = "status：1：待处理，2：正在处理，3：已处理", response = ResponseData.class)
    public ResponseData<ApiComplainVo> isComplain(

            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,


            @ApiParam(value = "订单id", name = "orderUuid", required = true) @RequestParam String orderUuid) {


        if (!StringUtils.isNotEmpty(orderUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "参数错误");
        }
        //司机查看投诉状态，取投诉乘客身份的记录
        return orderComplainService.findComplantStatus(orderUuid, CommonConstant.PASSENGER);
    }

    @RequestMapping(value = {"/token/order/waitFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "[专车&出租车]乘客超时等待费用接口", notes = "超时等待金额", response = ResponseData.class)
    public ResponseData<Map<String, Object>> waitFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(value = "订单id", name = "orderUuid", required = true) @RequestParam String orderUuid) {
        return orderFareService.getBeyondWaitFare(orderUuid);
    }

    @RequestMapping(value = {"/token/order/getAppointmentToBeStart"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机预约单提醒", notes = "出发时间半小时以内的一个预约单", response = ResponseData.class)
    public ResponseData<Map<String, Object>> getAppointmentToBeStart(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        Map<String, Object> orderMap = orderService.getAppointmentToBeStart(getUserUid(request)).getData();
        if (orderMap == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "无预约的订单");
        }
        return ResponseData.buildSuccessResponse("有出发时间在半个小时之内的预约单", orderMap);
    }

    @RequestMapping(value = {"/token/order/getAppointmentList"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机获取预约单列表", notes = "获取司机所有预约单信息", response = ResponseData.class)
    public ResponseData<List<JsonOrderListVo>> getAppointmentList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        Map<String, Object> paraMap = MapUtils.build(3);
        paraMap.put("driverUuid", getUserUid(request));
        return orderService.getAppointmentList(paraMap);
    }

    @RequestMapping(value = {"/token/order/getFreshAppointmentCount"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机获取可抢预约单数量", notes = "获取司机所有可抢预约单数量", response = ResponseData.class)
    public ResponseData<Map<String, Object>> getFreshAppointmentCount(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "areaCode", value = "区域代码", required = true) @RequestParam String areaCode,
            HttpServletRequest request) {
        Map<String, Object> paraMap = MapUtils.build(3);
        paraMap.put("driverUuid", getUserUid(request));
        paraMap.put("adCode", areaCode);
        return ResponseData.buildSuccessResponse("获取可抢预约单数量成功", orderService.getFreshAppointmentCount(paraMap).getData());
    }

    @RequestMapping(value = {"/token/order/getFreshAppointmentDetail"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机获取可抢预约单明细", notes = "获取司机所有可抢预约单明细", response = ResponseData.class)
    public ResponseData<List<JsonOrderListVo>> getFreshAppointmentDetail(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "areaCode", value = "区域代码", required = true) @RequestParam String areaCode,
            HttpServletRequest request) {
        Map<String, Object> paraMap = MapUtils.build(4);
        paraMap.put("driverUuid", getUserUid(request));
        paraMap.put("adCode", areaCode);
        return orderService.getFreshAppointmentDetail(paraMap);
    }

}
